/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @file TestUSBSerial.c
* 
* Function for testing the USB ( Serial ) library code.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <usbserial.cpp>

/**
* @example TestUSBSerial.c
*/

int main(int argc, char *argv[])
{
	
	if(argc<2)
	{
		puts("\nUSE:\nTestUSBSerial <type>\n");
		puts("types:");
		puts("\tread_bytes <port> <baud> <numbytes>");
                puts("\tread_until <port> <baud> <until>");
		puts("\tread_fromto <port> <baud> <from> <to>");
                puts("\twrite <port> <baud> <cadena>\n");
		puts("\t  e.g.");
		puts("\t    TestSocket server localhost 13000 => Waits on port 13000 a client tcp.");
		puts("\t    TestSocket client localhost 13000 \"a pair of words\" => Sends 'a pair of words' to a server tcp on localhost with port 13000 .\n");

		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"read_bytes")==0)
	{
		char* buffer;
		buffer=(char*)malloc(1024*sizeof(char));
		CMUsbSerial algo(argv[2],atoi(argv[3]));
		algo.read_bytes(buffer,atoi(argv[4]));
		printf("%s\n",buffer);
		free(buffer);
		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"read_until")==0)
	{
		char* buffer;
		buffer=(char*)malloc(1024*sizeof(char));
		CMUsbSerial algo(argv[2],atoi(argv[3]));
		algo.read_until(buffer,argv[4][0]);
		printf("%s\n",buffer);
		free(buffer);
		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"read_fromto")==0)
	{
		char* buffer;
		buffer=(char*)malloc(1024*sizeof(char));
		CMUsbSerial algo(argv[2],atoi(argv[3]));
		algo.read_fromto(buffer,argv[4][0],argv[5][0]);
		printf("%s\n",buffer);
		free(buffer);
		exit(EXIT_SUCCESS);
	}

	if(strcmp(argv[1],"write")==0)
	{
		CMUsbSerial algo(argv[2],atoi(argv[3]));
		algo.write_bytes(argv[4]);
		exit(EXIT_SUCCESS);
	}
	
}
